// pages/order/orderConfirmInfo/orderConfirmInfo.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contract_id: "",
    id: '',//识别id
    type: 1,//1、测试确认单、2保管清单、3分享确认单
    work_order_type: 1,//1试用2改造

    info: '',
    is_sign: false,//是否确认完了，是否能签字

    authInfo: { 'status_name': '待认证', status: '' },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var contract_id = options.contract_id||'';
    var id = options.id||'';//识别id
    var type = options.type || 1;
    var work_order_type = options.work_order_type || 1;
    that.setData({
      id,
      contract_id,
      type,
      work_order_type,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getInfo();
    that.getAuthInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.getInfo();
    that.getAuthInfo();
  },
  openFun(e){
    let { index } = e.currentTarget.dataset;
    let cookerArr = this.data.info.cooker_data;
    cookerArr[index].open = !cookerArr[index].open;
    let info = this.data.info;
    info.cooker_data = cookerArr;
    this.setData({ 
      info:info
     });
  },
  //获取确认单详情
  getInfo() {
    var that = this;
    app.ajax({
      url: 'User/Work/getEnergySavingData',
      method: "POST",
      data: {
        contract_id: that.data.contract_id,
        identification_id: that.data.id,
        type: that.data.type,
        work_order_type: that.data.work_order_type,
      },
      success: function (res) {
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          var data = res.data.data;
          if(that.data.type==1){
            data.cooker_data.forEach((item,index) => {
              data.cooker_data[index].open = true;
            })
          }
          that.setData({
            info: data
          })
          var dataList = data.cooker_data.data;
          if(that.data.type==2){
            var sureCount = 0;//累计已确认的数量
            dataList.forEach((item) => {
              if (item.is_confirm == 2) {
                sureCount += 1;
              }
            })
            if (sureCount == dataList.length) {
              that.setData({
                is_sign: true
              })
            }
          }
          
        }
      }
    })
  },

  //获取用户认证信息
  getAuthInfo() {
    var that = this;
    app.ajax({
      url: "User/Realname/getAuthInfo",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            authInfo: res.data.data
          })
        } else {
          that.setData({
            authInfo: { 'status_name': '待认证', 'status': '',}
          })
        }
      }
    })
  },

  //跳转我的认证
  authentication() {
    var that = this;
    if (that.data.authInfo.status == -2) {
      wx.navigateTo({
        url: '/pages/shopping/uploadDocuments/uploadDocuments',
      })
    } else if (that.data.authInfo.status == -3) {
      wx.navigateTo({
        url: '/pages/shopping/faceRecognition/faceRecognition',
      })
    } else if (that.data.authInfo.status == 2 || that.data.authInfo.status == 1) {
      wx.navigateTo({
        url: '/pages/myCertification/myCertification',
      })
    } else {
      wx.navigateTo({
        url: '/pages/shopping/realName/realName',
      })
    }
  },

  //单个确认炉具信息
  sureConfirm(e) {
    var that = this;
    if (that.data.authInfo.status != 2) {
      wx.showModal({
        content: '请先进行实名认证',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            //跳转认证
            that.authentication();
          }
        }
      })
    } else {
      var array_id = e.currentTarget.dataset.array_id;
      app.ajax({
        url: 'User/Work/CollectingInformationConfirm',
        method: "POST",
        data: {
          contract_id: that.data.contract_id,
          type: that.data.type,
          work_order_type: that.data.work_order_type,
          array_id: array_id,
        },
        success: function (res) {
          if (res.data.code == 1000) {
            app.showToast('确认成功');
            that.getInfo();//再次请求接口，刷新数据
          }else{
            app.showToast(res.data.message);
          }
        }
      })
    }
  },

  //去签字
  sign(){
    var that = this;
    if (that.data.authInfo.status!=2) {
      wx.showModal({
        content: '请先进行实名认证',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            //跳转认证
            that.authentication();
          }
        }
      })
    } else {
      var contract_id = that.data.contract_id;
      var type = that.data.type;//1、测试确认单、2保管清单、3分享确认单
      var work_order_type = that.data.work_order_type;//1试用2改造
      wx.navigateTo({
        url: '../sign/sign?contract_id=' + contract_id + '&type=' + type + '&work_order_type=' + work_order_type,
      })
    }
  },

  // 查看大图
  previewImage(e) {
    var currentImg = e.currentTarget.dataset.url;
    wx.previewImage({
      current: currentImg, // 当前显示图片的http链接
      urls: [currentImg] // 需要预览的图片http链接列表
    })
  },
})